package com.heima.search.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.behavior.pojo.ApBehaviorEntry;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.dtos.UserSearchDto;
import com.heima.model.search.pojos.ApUserSearch;
import com.heima.model.user.pojos.ApUser;
import com.heima.search.feign.BehaviorFeign;
import com.heima.search.mapper.UserSearchMapper;
import com.heima.search.service.UserSearchService;
import com.heima.utils.threadlocal.ApUserThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserSearchServiceImpl extends ServiceImpl<UserSearchMapper, ApUserSearch> implements UserSearchService {


    @Autowired
    BehaviorFeign behaviorFeign;
    /**
     * 保存用户搜索关键词
     * @param dto
     * @return
     * @throws Exception
     */
    @Async("taskExecutor")
    @Override
    public ResponseResult saveSearch(UserSearchDto dto) {
        //检查参数
        if (dto==null||dto.getSearchWords()==null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser apUser = ApUserThreadLocalUtils.getUser();

        if (dto.getEquipmentId()==null&&apUser==null){
           return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        if (dto.getEquipmentId()==null){
            if (apUser.getId()==0){
                dto.setEquipmentId(Integer.parseInt(apUser.getPhone()));
            }else {
                dto.setEquipmentId(apUser.getId());
            }
        }
        ApBehaviorEntry behaviorEntry = behaviorFeign.findByUserId(dto.getEquipmentId());
        if (behaviorEntry==null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUserSearch apUserSearch = getOne(Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getEntryId, behaviorEntry.getId()).eq(ApUserSearch::getKeyword, dto.getSearchWords()));
       //如果等于null则创建
        if (apUserSearch==null){
            //保存关键词
             apUserSearch=new ApUserSearch();
            apUserSearch.setCreatedTime(new Date());
            apUserSearch.setEntryId(behaviorEntry.getId());
            apUserSearch.setKeyword(dto.getSearchWords());

        }
        //否则改变状态为有效
        apUserSearch.setStatus(1);

        return ResponseResult.okResult( save(apUserSearch));
    }


    /**
     * 删除用户搜索词
     * @param dto
     * @return
     * @throws Exception
     */
    @Override
    public ResponseResult delSearch(UserSearchDto dto) {
        if (dto==null||dto.getSearchWords()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        ApUser apUser = ApUserThreadLocalUtils.getUser();
        if (apUser==null&&dto.getEquipmentId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        if (dto.getEquipmentId()==null){
            if (apUser.getId()==0){
                dto.setEquipmentId(Integer.parseInt(apUser.getPhone()));
            }else {
                dto.setEquipmentId(apUser.getId());
            }

        }
        ApUserSearch userSearch = getOne(Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getEntryId, dto.getEquipmentId()).eq(ApUserSearch::getKeyword, dto.getSearchWords()));
        if (userSearch==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        return ResponseResult.okResult(removeById(userSearch));
    }
}
